package com.ruoyi.web.controller.system;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysBook;
import com.ruoyi.system.service.ISysBookService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 图书Controller
 * 
 * @author 杨正
 * @date 2021-10-15
 */
@RestController
@RequestMapping("/system/book")
public class SysBookController extends BaseController
{
    @Autowired
    private ISysBookService sysBookService;

    /**
     * 查询图书列表
     */
    @PreAuthorize("@ss.hasPermi('system:book:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysBook sysBook)
    {
        startPage();
        List<SysBook> list = sysBookService.selectSysBookList(sysBook);
        return getDataTable(list);
    }

    /**
     * 导出图书列表
     */
    @PreAuthorize("@ss.hasPermi('system:book:export')")
    @Log(title = "图书", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysBook sysBook)
    {
        List<SysBook> list = sysBookService.selectSysBookList(sysBook);
        ExcelUtil<SysBook> util = new ExcelUtil<SysBook>(SysBook.class);
        return util.exportExcel(list, "图书数据");
    }

    /**
     * 获取图书详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:book:query')")
    @GetMapping(value = "/{bookId}")
    public AjaxResult getInfo(@PathVariable("bookId") Integer bookId)
    {
        return AjaxResult.success(sysBookService.selectSysBookByBookId(bookId));
    }

    /**
     * 新增图书
     */
    @PreAuthorize("@ss.hasPermi('system:book:add')")
    @Log(title = "图书", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysBook sysBook)
    {

        sysBook.setCreateBy(getUsername());
        return toAjax(sysBookService.insertSysBook(sysBook));
    }

    /**
     * 修改图书
     */
    @PreAuthorize("@ss.hasPermi('system:book:edit')")
    @Log(title = "图书", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysBook sysBook)
    {
        if (!sysBook.getDelFlag().equals("0")){
            return AjaxResult.error("错误");
        }else {

            sysBook.setUpdateBy(getUsername());
            return toAjax(sysBookService.updateSysBook(sysBook));
        }
    }

    /**
     * 删除图书
     */
    @PreAuthorize("@ss.hasPermi('system:book:remove')")
    @Log(title = "图书", businessType = BusinessType.DELETE)
	@DeleteMapping("/{bookIds}")
    public AjaxResult remove(@PathVariable Integer[] bookIds)
    {
        return toAjax(sysBookService.deleteSysBookByBookIds(bookIds));
    }

    /**
     * 出库
     */
    @PreAuthorize("@ss.hasPermi('system:book:gostatus')")
    //@Log(title = "图书", businessType = BusinessType.DELETE)
    @PutMapping("/{bookIds}")
    public AjaxResult status(@PathVariable Integer[] bookIds)
    {
        return toAjax(sysBookService.statusSysBookByBookIds(bookIds));
    }

}
